1   /*
2    * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /* @test
25   * @bug 4173960
26   * @summary synopsis: Activatable objects cannot be restarted.
27   * @author Laird Dornin
28   *
29   * @library ../../../testlibrary
30   * @build ActivateMe
31   * @build ForceLogSnapshot
32   * @build ForceLogSnapshot_Stub
33   * @run main/othervm/policy=security.policy/timeout=640 ForceLogSnapshot
34   */
35  
36  import java.io.*;
37  import java.rmi.*;
38  import java.rmi.activation.*;
39  import java.rmi.server.*;
40  import java.rmi.registry.*;
41  import java.util.*;
42  
43  public class ForceLogSnapshot
44          implements ActivateMe
45  {
46      /** how many activatable remote objects to create to test rmid */
47      final public static int HOW_MANY = 50;
48      final public static int NUM_GROUPS = 4;
49      /** cause RMID to generate a snapshot every 10th activated object */
50      final public static int SNAPSHOT_INTERVAL = 10;
51  
52      private ActivationID id;
53      private Vector responders = new Vector();
54  
55      private static final String RESTARTABLE = "restartable";
56      private static final String ACTIVATABLE = "activatable";
57  
58      private static Object lock = new Object();
59      private static boolean[] restartedObjects = new boolean[HOW_MANY];
60      private static boolean[] activatedObjects = new boolean[HOW_MANY];
61  
62      public ForceLogSnapshot(ActivationID id, MarshalledObject mobj)
63          throws ActivationException, RemoteException
64      {
65          this.id = id;
66          int intId = 0;
67  
68          Activatable.exportObject(this, id, 0);
69          ActivateMe obj;
70          String responder;
71          try {
72              Object[] stuff = (Object[]) mobj.get();
73  
74              intId = ((Integer) stuff[0]).intValue();
75              responder = (String) stuff[1];
76              obj = (ActivateMe) stuff[2];
77  
78              System.err.println(responder + " service started");
79          } catch (Exception e) {
80              System.err.println("unable to obtain stub from marshalled object");
81              System.err.println(e.getMessage());
82              e.printStackTrace();
83              return;
84          }
85  
86          obj.ping(intId, responder);
87      }
88  
89      public ForceLogSnapshot() throws RemoteException {
90          UnicastRemoteObject.exportObject(this, 0);
91      }
92  
93      public void ping(int intId, String responder) {
94          System.err.println("ForceLogSnapshot: received ping from " +
95                             responder);
96          if (responder.equals(RESTARTABLE)) {
97              synchronized (lock) {
98                  restartedObjects[intId] = true;
99              }
100         } else if (responder.equals(ACTIVATABLE)) {
101             synchronized (lock) {
102                 activatedObjects[intId] = true;
103             }
104         }
105     }
106 
107     public void crash() {
108         System.exit(0);
109     }
110 
111     public ActivationID getID() {
112         return id;
113     }
114 
115     public static void main(String[] args) {
116 
117         System.out.println("\nRegression test for bug 4173960\n");
118 
119         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
120 
121         RMID rmid = null;
122         ForceLogSnapshot[] unicastObjs = new ForceLogSnapshot[HOW_MANY];
123 
124         try {
125             String option = " -Dsun.rmi.activation.snapshotInterval=" +
126                 SNAPSHOT_INTERVAL;
127 
128             RMID.removeLog();
129             rmid = RMID.createRMID();
130             rmid.addOptions(new String[] {option, "-Djava.compiler="});
131             rmid.start();
132 
133             /* Cause activation groups to have a security policy that will
134              * allow security managers to be downloaded and installed
135              */
136             Properties p = new Properties();
137             // this test must always set policies/managers in its
138             // activation groups
139             p.put("java.security.policy",
140                   TestParams.defaultGroupPolicy);
141             p.put("java.security.manager",
142                   TestParams.defaultSecurityManager);
143 
144             Object[][] stuff = new Object[HOW_MANY][];
145             MarshalledObject restartMobj = null;
146             ActivationGroupDesc groupDesc = null;
147             MarshalledObject activateMobj = null;
148             ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS];
149             ActivationDesc restartableDesc = null;
150             ActivationDesc activatableDesc = null;
151             ActivateMe[] restartableObj = new ActivateMe[HOW_MANY];
152             ActivateMe[] activatableObj = new ActivateMe[HOW_MANY];
153 
154             /*
155              * Create unicast object to be contacted when service is activated.
156              */
157             int group = 0;
158             int groupNo = 0;
159             for (int i = 0 ; i < HOW_MANY ; i ++ ) {
160 
161                 System.err.println("Creating descriptors and remote objects");
162 
163                 unicastObjs[i] = new ForceLogSnapshot();
164 
165                 /*
166                  * Create and register descriptors for a restartable and
167                  * non-restartable service (respectively) in a group other than
168                  * this VM's group.
169                  */
170                 stuff[i] = new Object[] { new Integer(i),
171                                               RESTARTABLE, unicastObjs[i] };
172                 restartMobj = new MarshalledObject(stuff[i]);
173 
174                 stuff[i][1] = ACTIVATABLE;
175                 activateMobj = new MarshalledObject(stuff[i]);
176 
177                 groupDesc =
178                     new ActivationGroupDesc(p, null);
179 
180                 if (i < NUM_GROUPS) {
181                     groupNo = i;
182                     groupIDs[groupNo] =
183                         ActivationGroup.getSystem().
184                         registerGroup(groupDesc);
185                 } else {
186                     groupNo = (group++)%NUM_GROUPS;
187                 }
188 
189                 System.err.println("Objects group number: " + groupNo);
190 
191                 restartableDesc =
192                     new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
193                                        restartMobj, true);
194 
195                 activatableDesc =
196                     new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
197                                        activateMobj, false);
198 
199                 System.err.println("Registering descriptors");
200                 restartableObj[i] =
201                     (ActivateMe) Activatable.register(restartableDesc);
202 
203                 activatableObj[i] =
204                     (ActivateMe) Activatable.register(activatableDesc);
205                 System.err.println("registered activatable #: " + i);
206 
207                 // start reusing groups if we need to do so.
208             }
209 
210             int repeatOnce = 1;
211             do {
212 
213                 /*
214                  * Restart rmid; it should start up the restartable service
215                  */
216                 rmid.restart();
217 
218                 if (howManyRestarted(restartedObjects, 10) < HOW_MANY) {
219                         TestLibrary.bomb("Test1 failed: a service would not " +
220                                          "restart");
221                 }
222                 System.err.println("Test1 passed: rmid " +
223                                    "all service(s) restarted. Performing next test.");
224 
225                 /*
226                  * Make sure no activatable services were automatically
227                  * restarted.
228                  */
229                 if (howManyRestarted(activatedObjects, 2) != 0) {
230                     TestLibrary.bomb("Test2 failed: activatable service restarted!",
231                                      null);
232                 }
233                 System.err.println("Test2 passed: rmid did not " +
234                                    "restart activatable service(s)");
235 
236                 if (repeatOnce > 0) {
237                     try {
238                         System.err.println("\nCrash restartable object");
239                         for (int i = 0 ; i < HOW_MANY ; i ++) {
240                             restartableObj[i].crash();
241                         }
242                     } catch (Exception e) {
243                     }
244                 }
245 
246             } while (repeatOnce-- > 0);
247 
248 
249         } catch (Exception e) {
250             TestLibrary.bomb("test failed", e);
251         } finally {
252             ActivationLibrary.rmidCleanup(rmid);
253             for (int i = 0 ; i < HOW_MANY ; i ++) {
254                 TestLibrary.unexport(unicastObjs[i]);
255             }
256         }
257     }
258 
259     /**
260      * Check to see how many services have been automatically
261      * restarted.
262      */
263     private static int howManyRestarted(boolean[] startedObjects, int retries) {
264         int succeeded = 0;
265         int restarted = 0;
266         int atry = 0;
267 
268         while ((restarted < HOW_MANY) && (atry < retries)) {
269             restarted = 0;
270             for (int j = 0 ; j < HOW_MANY ; j ++ ) {
271                 synchronized(lock) {
272                     if (startedObjects[j]) {
273                         restarted ++;
274                     }
275                 }
276             }
277             System.err.println("not all objects restarted, retrying...");
278             try {
279                 Thread.sleep(10000);
280             } catch (InterruptedException ie) {
281             }
282             atry ++;
283         }
284         return restarted;
285     }
286 }